#!/bin/sh
# create -v3 logs and SVG from dwg's with dwg2SVG
srcdir="@srcdir@"
top_builddir="@top_builddir@"
timeout="@TIMEOUT_10@"
TESTS_ENVIRONMENT="@AM_TESTS_ENVIRONMENT@"

if [ -n "$VALGRIND" ] && [ -n "$LOG_COMPILER" ]; then
    TESTPROG="$LOG_COMPILER $LOG_FLAGS"
elif [ -n "@WINE@" ]; then
    TESTPROG="@WINE@"
elif [ -n "$TESTS_ENVIRONMENT" ]; then
    TESTPROG="$TESTS_ENVIRONMENT @LTEXEC@"
else
    TESTPROG="@LTEXEC@"
fi

make -s -C src
make -s -C programs dwg2SVG
if [ -n "@DSYMUTIL@" ]; then
    @DSYMUTIL@ programs/dwg2SVG
fi
v=-v3
# dummy to please shellcheck
test -z "$v" && echo $top_builddir
out=
ob="$b"
gerr=0
case "$CFLAGS" in
*-DDEBUG_CLASSES*) timeout="" ;;
*) ;;
esac

for d in "$@"; do
    case $d in
    -v*)
        v="$d"
        ;;
    *)
        if [ ! -f "$d" ]; then
            if [ -f "$srcdir/test/test-data/$d" ]; then
                d="$srcdir/test/test-data/$d"
            else
                if [ -f "$srcdir/test/test-data/$d.dwg" ]; then
                    d="$srcdir/test/test-data/$d.dwg"
                fi
            fi
        fi
        xd=$(dirname "$d")
        dir=_$(basename "$xd")
        if [ "$dir" = "_test-data" ]; then dir=""; fi
        if [ "$dir" = "_td" ]; then dir=""; fi
        if [ "$dir" = "_." ]; then dir=""; fi
        b=$(basename "$d" .dwg)
        log="$b$dir.svg.log"
        out="$b$dir.svg"
        echo "$timeout $TESTPROG programs/dwg2SVG $v $d \>$out 2\>$log"
        # shellcheck disable=SC2086
        $timeout $TESTPROG programs/dwg2SVG "$v" "$d" >"$out" 2>"$log"
        err=$?
        [ $err -gt $gerr ] && gerr=$err
        tail -n2 "$log"
        echo "@RUN_JING@ $out"
        @RUN_JING@ "$out"
        ;;
    esac
done
v=
b="$ob"
if [ $gerr -gt 128 ]; then exit 1; fi
